package com.example.pre.controller.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.pre.model.SystemLog.SystemLog;
import com.example.pre.tools.JsonConvert;
import com.example.pre.tools.config.SystemConfig;

@RequestMapping("/demo")
public class ControllerBase {

	/**
	 * request
	 */
	@Autowired
	protected HttpServletRequest request;
	/**
	 * response
	 */
	@Autowired
	protected HttpServletResponse response;

	@Autowired
	private SystemConfig systemConfig;

	/**
	 * 获取请求的Ip地址
	 * 
	 * @return ip地址
	 */
	protected String getIp() {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_CLIENT_IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("HTTP_X_FORWARDED_FOR");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	/**
	 * 获取用户类型
	 * 
	 * @return 用户类型
	 */
	protected String getUserType() {
		return request.getHeader("User-Type");
	}

	/**
	 * 获取用户id
	 * 
	 * @return 用户id
	 */
	protected Integer getUserId() {
		Integer userId = null;
		try {
			userId = Integer.valueOf(request.getHeader("User-Id"));
			return userId;
		} catch (Exception e) {
			return userId;
		}
	}

	/**
	 * 生成后台系统日志,用户ERROR日志的生成
	 * 
	 * @param e       错误类
	 * @param message 日志信息
	 * @param start   方法进入时间
	 * @param eventId 事件id
	 * @return AosaSystemLog JSON字符串
	 */
	public String createLog(Exception e, String message, Long start, String eventId) {
		long end = System.currentTimeMillis();
		StackTraceElement[] traces = e.getStackTrace();
		StringBuffer stringBuffer = new StringBuffer();
		int i = 0;
		for (StackTraceElement stackTraceElement : traces) {
			i++;
			if (stringBuffer.length() != 0) {
				stringBuffer.append(";");
				stringBuffer.append(stackTraceElement.toString());
				continue;
			}
			stringBuffer.append(stackTraceElement.toString());
			if (i > 5) {
				break;
			}
		}
		String traceStr = stringBuffer.toString();
		SystemLog systemLog = new SystemLog();
		systemLog.setCaller(systemConfig.getSystemName() + "-Controller");
		systemLog.setEventId(eventId);
		systemLog.setMessage(message + ",堆栈信息:" + traceStr);
		if (end - start < 0) {
			systemLog.setResponseTime(-1L);
		} else {
			systemLog.setResponseTime(end - start);
		}
		systemLog.setSystemName(systemConfig.getSystemName());
		return JsonConvert.getJsonArrStrFromList(systemLog);
	}

	/**
	 * 生成后台系统日志,用于INFO日志的生成
	 * 
	 * @param message 日志信息
	 * @param start   方法进入时间
	 * @param eventId 事件id
	 * @return AosaSystemLog JSON字符串
	 */
	public String createLog(String message, Long start, String eventId) {
		long end = System.currentTimeMillis();
		SystemLog systemLog = new SystemLog();
		systemLog.setCaller(systemConfig.getSystemName() + "-Controller");
		systemLog.setEventId(eventId);
		systemLog.setMessage(message);
		if (end - start < 0) {
			systemLog.setResponseTime(-1L);
		} else {
			systemLog.setResponseTime(end - start);
		}

		systemLog.setSystemName(systemConfig.getSystemName());
		return JsonConvert.getJsonArrStrFromList(systemLog);
	}

}
